import rclpy
from rclpy.node import Node
from tf2_ros import TransformListener
from tf2_ros.buffer import Buffer
from rclpy.time import Time
#监听器或rviz2会订阅话题tf_stati，tf的消息，然后监听器把结果转换给buffer缓存器
class TFListenerPy(Node):
    def __init__(self):
        super().__init__("tf_listener_node")
        self.buffer=Buffer()
        self.tl=TransformListener(self.buffer,self)
        self.timer=self.create_timer(1.0,self.on_timer)
    
    def on_timer(self):
        if self.buffer.can_transform("camera","laser",Time()):
            ts=self.buffer.lookup_transform("camera","laser",Time())
            self.get_logger().info("------转换后的数据------")
            self.get_logger().info(f"开始转换，父坐标系为{ts.header.frame_id},子坐标系为{ts.child_frame_id},偏移量({ts.transform.translation.x},{ts.transform.translation.y},{ts.transform.translation.z})")
        else:
            self.get_logger().info("转换失败")

def main():
    rclpy.init()
    rclpy.spin(TFListenerPy())
    rclpy.shutdown()
if __name__=='__main__':
    main()
